<template>
	<view class="bg">
		<view class="p_flex5 my_top">
			<view class="user_id">用户ID:{{userId}}</view>
			<view class="yu_e">
				<text class="p_font_28 yu_e_title">可用余额</text>
				<text class="p_font_44 yu_e_value">{{balance}}</text>
			</view>
			<view class="withdraw_btn" @click="userWithdrawal">提现</view>		
		</view>
		<view class="withdrawal_list" v-if="withdrawalList.length">
			<view class="withdrawal_list_item" :key="index" v-for="(item,index) in withdrawalList">
				<view class="left">
					<view class="status">
						{{getStatus(item.status)}}
					</view>
					<view class="time">
						{{item.create_time}}
					</view>
				</view>
				<view class="right">
					{{item.price}}
				</view>
				<view v-if="item.status == '4'" class="confirm-btn" @click="confirmWithdrawal(item)">确认收款</view>		
			</view>
		</view>
		<view class="no_data" v-else>
			<image src="https://dy.sykj365.com/uploads/mini/zanwu@2x.png" mode="widthFix"></image>
			<text>暂无提现记录~</text>
		</view>
	</view>	
</template>

<script>
	export default {
		data() {
			return {
				userId:'',
				balance:'',
				withdrawalList: []
			}
		},
		onReady() {
			
		},
		onLoad() {
			this.getList();
		},
		onShow() {
			this.getUserinfo();
		},
		methods: {
			userWithdrawal() {
				if(!Number(this.balance) > 0 ){
					return false;
				}
				uni.showLoading();
				this.$http.post(this.$api.userWithdrawal,{
					price: this.balance
				}).then(res => {
					if(res.code === 20000){
						uni.hideLoading();
						this.getUserinfo();
						this.getList();
						getApp().getMsg(res.msg)
					}					
				})
			},
			getList() {
				this.$http.get(this.$api.withdrawalList).then(res => {
					if(res.code === 20000){
						this.withdrawalList = res.data;
					}					
				})
			},
			getUserinfo() {
				this.$http.post(this.$api.userinfo).then(res => {
					if(res.code === 20000){
						this.balance = res.data.balance ? res.data.balance : 0;
						this.userId = res.data.id;
					}					
				})
			},
			getStatus(val) {
				switch(val){
					case '0':
					  return '提现中';
					case '1':
					  return '已提现';
					case '2':
					  return '提现失败';
					case '3':
					  return '驳回';
					case '4':
					  return '待确认';
					default:
					  return '';
				}
			},
			confirmWithdrawal(val) {
				if (wx.canIUse('requestMerchantTransfer')) {
				  wx.requestMerchantTransfer({
				    mchId: "1659550367",
				    appId: wx.getAccountInfoSync().miniProgram.appId,
				    package: val.package_info,
				    success: (res) => {
						getList();
						getApp().getMsg('提现成功', 'success');
				    },
				    fail: (res) => {
				      console.log('fail:', res);
					  getApp().getMsg('提现失败', 'error');
				    },
				  });
				} else {
				  wx.showModal({
				    content: '你的微信版本过低，请更新至最新版本。',
				    showCancel: false,
				  });
				}
			}
		}
	}
</script>

<style lang="less">
	.bg {
		min-height: 100vh;
		padding-bottom: 48rpx;
		background-color: #F4F5FA;
	}
	.my_top {
		display: flex;
		justify-content: space-between;
		height: 300rpx;
		background-image: url("https://dy.sykj365.com/uploads/mini/bg-top.jpg");
		background-size: 100% 100%;
		.user_id {
			margin-left: 51rpx;
			margin-top: 68rpx;
			font-weight: 500;
			font-size: 28rpx;
			color: #999999;
			opacity: 0.8;
		}
		.yu_e {
			margin-top: 60rpx;
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: flex-start;
			text-align: center;
			.yu_e_title {
				font-size: 28rpx;
				color: #666666;
				opacity: 0.8;
			}
			.yu_e_value {
				margin-top: 34rpx;
				font-weight: 800;
				font-size: 60rpx;
				color: #333333;
			}
		}
	}
	
	.withdraw_btn {
		margin-top: 91rpx;
		width: 139rpx;
		height: 60rpx;
		background: #009353;
		border-radius: 60rpx 0 0 60rpx;
		border: 2px solid #009353;
		text-align: center;
		line-height: 60rpx;
		font-size: 26rpx;
		color: #fff;
	}
	.withdrawal_list {
		width: calc(100% - 60rpx);
		margin: -52rpx auto 0;
		background-color: #fff;
		border-radius: 10rpx;
		.withdrawal_list_item {
			width: calc(100% - 66rpx);
			margin: 0 auto;
			padding: 41rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #E4E4E4;
			.left {
				.status {
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					line-height: 40rpx;
				}
				.time {
					font-weight: 500;
					font-size: 24rpx;
					color: #ADADAD;
					line-height: 40rpx;
				}
			}
			.right {
				font-weight: bold;
				font-size: 36rpx;
				color: #F82300;
				line-height: 40rpx;
			}
			&:last-child {
				border: none;
			}
		}
	}
	.no_data {
		text-align: center;
		color: #B2B2B2;
		font-size: 28rpx;
		image {
			width: 192rpx;
			height: 176rpx;
			margin: 168rpx auto 48rpx;
		}
	}
	
	.confirm-btn {
		background: #05B570;
		border-radius: 56rpx 56rpx 56rpx 56rpx;
		padding: 16rpx 30rpx;
		color: #FFFFFF;
		font-size: 28rpx;
	}
	
</style>